package com.wmz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wmz.domain.SysMenu;
import com.wmz.domain.SysUser;
import com.wmz.model.Result;
import com.wmz.service.SysUserService;
import com.wmz.utils.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author wmz
 * @date 2024/7/8 23:48
 * @description 系统管理员控制层
 */
@Api(tags = "系统管理员接口管理")
@Component
@RestController
@RequestMapping("sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("查询登录的用户信息")
    @GetMapping("info")
    public Result<SysUser> loadSysUserInfo(){
        // 获取用户Id
        Long userId = AuthUtils.getLoginUserId();
        // 根据用户标识进行查询，mbp自带的查询
        SysUser sysUser = sysUserService.getById(userId);
        return Result.success(sysUser);
    }
    @ApiOperation("多条件分页查询系统管理员")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public Result<Page<SysMenu>> loadSysUserPage(@RequestParam Long currentPage,
                                                 @RequestParam Long size,
                                                 @RequestParam(required = false) String username){
        Page page = new Page(currentPage,size);
        page = sysUserService.page(page, new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(username), SysUser::getUsername, username)
                .orderByDesc(SysUser::getCreateTime)
        );
        return Result.success(page);
    }
    @ApiOperation("新增管理员")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result saveSysUser(@RequestBody SysUser sysUser){
        int count = sysUserService.saveSysUser(sysUser);
        return Result.handle(count > 0);
    }
    @ApiOperation("根据标识查询系统管理员信息")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserInfo(@PathVariable Long id){
        SysUser sysUser = sysUserService.querySysUserInfo(id);
        return Result.success(sysUser);
    }
    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUserInfo(@RequestBody SysUser sysUser){
        int count = sysUserService.modifySysUserInfo(sysUser);
        return Result.handle(count > 0);
    }
    @ApiOperation("删除管理员")
    @DeleteMapping("{idList}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> removeSysUser(@PathVariable Long[] idList){
        Boolean count = sysUserService.removeByIdList(idList);
        return Result.handle(count);
    }
}
